package com.hh.item.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hh.item.dao.IMessageDao;
import com.hh.item.pojo.Message;
import com.hh.item.pojo.User;
import com.hh.item.service.IMessageService;
import com.hh.item.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author yangfan
 * @date 2020/3/8 19:37
 * @description
 */
@Service("messageService")
@Transactional
public class MessageServiceImpl implements IMessageService {

    @Autowired
    private IMessageDao messageDao;
    @Autowired
    private IUserService userService;

    /**
     * @param userId
     * @return java.util.List<com.hh.item.pojo.Message>
     * @description 分页获取用户主页微博（关注人/自己）
     */
    @Override
    public PageInfo<Message> mainMsgs(Integer userId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Message> messages = messageDao.mainMsgs(userId);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        for (Message message : messages) {
            message.setUserRelation(this.getMsgUserRela(userId, message.getMsgId()));
            message.setMsgTimeStr(simpleDateFormat.format(message.getMsgTime()));
        }
        return PageInfo.of(messages);
    }

    @Override
    public Integer getMsgUserRela(Integer userId, Integer msgId) {
        Integer rela = -1;
        List<Integer> relas = messageDao.getRelationStatus(userId, msgId);
        if (!relas.isEmpty()) {
            if (relas.contains(0)) {
                rela = 0;
                if (relas.contains(1)) {
                    rela = 2;
                }
            } else if (relas.contains(1)) {
                rela = 1;
            }
        }
        return rela;
    }

    /**
     * @param message
     * @return java.lang.String
     * @description 用户发原创微博业务逻辑 同时更新用户总微博数
     */
    @Override
    public String sent(Message message) {
        User user = new User();
        user.setUserId(message.getUserId());
        user.setMsgCount(this.selectMsgCountByUserId(message.getUserId()) + 1);

        if ("true".equals(userService.update(user))) {
            message.setMsgCollect(0);
            message.setMsgLike(0);
            message.setMsgTime(new Date());
            if (messageDao.insertSelective(message) == 1) {
                return "true";
            }
        }
        return "false";
    }

    /**
     * @param userId
     * @return java.lang.Integer
     * @description 根据用户id查询该用户微博数
     */
    @Override
    public Integer selectMsgCountByUserId(Integer userId) {
        return messageDao.selectMsgCountByUserId(userId);
    }

    /**
     * @param userId
     * @param pageNum
     * @param pageSize
     * @return com.github.pagehelper.PageInfo<com.hh.item.pojo.Message>
     * @description 获取用户微博
     */
    @Override
    public PageInfo<Message> getMsgs(Integer userId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Message> messages = messageDao.getMsgs(userId);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        for (Message message : messages) {
            message.setUserRelation(this.getMsgUserRela(userId, message.getMsgId()));
            message.setMsgTimeStr(simpleDateFormat.format(message.getMsgTime()));
        }
        return PageInfo.of(messages);
    }

    /**
     * @param userId
     * @param msgId
     * @return java.lang.String
     * @description 点赞与取消点赞
     */
    @Override
    public String like(Integer userId, Integer msgId) {
        String result = "false";

        boolean alreadyLike = messageDao.getRelationStatus(userId, msgId).contains(1);
        if (alreadyLike) {
            int isCancelLike = messageDao.cancelLike(msgId);
            int isUnLike = messageDao.unLike(userId, msgId);
            if (isCancelLike == 1 && isUnLike == 1) {
                result = "true";
            }
        } else {
            int isAddLike = messageDao.addLike(msgId);
            int isLike = messageDao.like(userId, msgId);
            if (isAddLike == 1 && isLike == 1) {
                result = "true";
            }
        }
        return result;
    }

    /**
     * @param userId
     * @param msgId
     * @return java.lang.String
     * @description 收藏与取消收藏
     */
    @Override
    public String collect(Integer userId, Integer msgId) {
        String result = "false";

        boolean alreadyCollect = messageDao.getRelationStatus(userId, msgId).contains(0);
        if (alreadyCollect) {
            int isCancelLike = messageDao.cancelCollect(msgId);
            int isUnLike = messageDao.unCollect(userId, msgId);
            if (isCancelLike == 1 && isUnLike == 1) {
                result = "true";
            }
        } else {
            int isAddLike = messageDao.addCollect(msgId);
            int isLike = messageDao.Collect(userId, msgId);
            if (isAddLike == 1 && isLike == 1) {
                result = "true";
            }
        }
        return result;
    }

    /**
     * @param userId
     * @param msgId
     * @return java.lang.String
     * @description 删除微博
     */
    @Override
    public String delete(Integer userId, Integer msgId) {
        User user = new User();
        user.setUserId(userId);
        user.setMsgCount(this.selectMsgCountByUserId(userId) - 1);

        if ("true".equals(userService.update(user))) {
            Message message = new Message();
            message.setMsgId(msgId);
            if (messageDao.deleteByPrimaryKey(message) == 1) {
                return "true";
            }
        }
        return "false";
    }

    @Override
    public List<Message> myCollect(Integer userId) {
        List<Message> messages = messageDao.collectMsgs(userId);
        return messages;
    }

    @Override
    public PageInfo<Message> onesMsgs(Integer targetId, Integer userId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Message> messages = messageDao.getMsgs(targetId);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        User user = null;
        Integer rela = null;
        if (messages.size() > 0) {
            user = userService.getMine(targetId);
            user.setUserPwd(null);
            rela = this.getMsgUserRela(userId, messages.get(0).getMsgId());
        }
        for (Message message : messages) {
            message.setUser(user);
            message.setUserRelation(rela);
            message.setMsgTimeStr(simpleDateFormat.format(message.getMsgTime()));
        }
        return PageInfo.of(messages);
    }


}
